package com.cityfood.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cityfood.mapper.CfUserCollectMapper;
import com.cityfood.pojo.CfUserCollect;
import com.cityfood.pojo.CfUserCollectExample;
import com.cityfood.pojo.CfUserCollectExample.Criteria;
import com.cityfood.service.UserCollectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class UserCollectServiceImpl implements UserCollectService {
	
	@Autowired
	private CfUserCollectMapper UserCollectMapper;

	@Override
	public List<CfUserCollect> getCollectByUserId(Integer userId) {
		CfUserCollectExample example = new CfUserCollectExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		List<CfUserCollect> list = UserCollectMapper.selectByExample(example);
		return list;
	}
	
	@Override
	public PageInfo<CfUserCollect> getCollectByUserIdAndPage(Integer userId, Integer currentPage) {
		
		//设置分页的页数和行数
		PageHelper.startPage(currentPage, 10);
		
		CfUserCollectExample example = new CfUserCollectExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		List<CfUserCollect> list = UserCollectMapper.selectByExample(example);
		
		//5.获取分页信息
		PageInfo<CfUserCollect> collectListPage = new PageInfo<>(list);
		return collectListPage;
	}
	
	@Override
	public int insertCollect(CfUserCollect collect) {
		int i = UserCollectMapper.insertSelective(collect);
		return i;
	}

	@Override
	public int deleteCollectByUserId(Integer userId,Integer productId) {
		CfUserCollectExample example = new CfUserCollectExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		criteria.andProductIdEqualTo(productId);
		int i = UserCollectMapper.deleteByExample(example);
		return i;
	}

	@Override
	public CfUserCollect getCollectByUserIdAndProductId(Integer userId, Integer productId) {
		CfUserCollectExample example = new CfUserCollectExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		criteria.andProductIdEqualTo(productId);
		List<CfUserCollect> list = UserCollectMapper.selectByExample(example);
		if (list.size()>0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public int updateCollect(CfUserCollect collect) {
		int i = UserCollectMapper.updateByPrimaryKeySelective(collect);
		return i;
	}

}
